/*=========================================================================

Program:   Virtual Surgery
Language:  C++
Date:      2009-9-1
Version:   0.1
Mail:		   nelfylee@gmail.com

Copyright (c) 2009 Nelfy Lee. All rights reserved.

=========================================================================*/
#ifndef _LOAD_DATA_THREAD_H_
#define _LOAD_DATA_THREAD_H_

#include "itkCommand.h"
#include "itkProcessObject.h"
#include "itkImageDuplicator.h"

#include <QThread>
#include <QString>
#include <QFileDialog>

class PipelineModel;

class LoadDataThreadBase : public QThread
{
	Q_OBJECT

public:
    typedef itk::MemberCommand< LoadDataThreadBase>  ProgressCommandType;
    
	LoadDataThreadBase(const QString &filename, PipelineModel *pipeline)
	{
		this->filename = filename;
		this->m_pipeline = pipeline;
	}

protected:
	virtual void ProcessEvent(itk::Object *caller, const itk::EventObject &event)
	{
		if( typeid(itk::ProgressEvent) == typeid(event))
		{
			::itk::ProcessObject::Pointer process =
				dynamic_cast< itk::ProcessObject *>(caller);

			emit(emitProgressUpdate(process->GetProgress()*100));
		}
	}

	virtual void run(){}


signals:
	void emitProgressStart(QString);
	void emitProgressUpdate(int);
	void emitProgressDone();

protected:
	QString filename;
	PipelineModel *m_pipeline;
};




template<class CImageType>
class LoadDataThread : public LoadDataThreadBase
{

public:

	LoadDataThread(const QString &filename, PipelineModel *pipeline) 
		: LoadDataThreadBase(filename, pipeline)
	{

	}

	typename CImageType::Pointer getOutput()
	{
		typedef itk::ImageDuplicator<CImageType> ImageDuplicator;
		typename ImageDuplicator::Pointer dup = ImageDuplicator::New();

		dup->SetInputImage(m_image);
		dup->Update();
			
		return dup->GetOutput();
	}


protected:
	typename CImageType::Pointer m_image;

};







#endif






